package com.piao.springboot_jpa.controller;

import com.piao.springboot_jpa.entity.Word;
import com.piao.springboot_jpa.repository.WordRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class JPAController {

    //注入业务对象 WordRepository
    @Autowired
    WordRepository wordRepository;
    //存储预返回页面的结果对象
    private Map<String, Object> result;


    //添加单词信息
    @PostMapping("/insertWord")
    @ResponseBody
    public Map<String, Object> insertWord(Word word) {
        result = new HashMap<>();

        //数据校验 判空！
        if (null == word) {
            result.put("message", "插入的单词为空");
            return result;
        }
        if (null == word.getChinese() || "".equals(word.getEnglish())) {
            result.put("message", "插入的单词中文为空或者为空字符串");
            return result;
        }
        if (null == word.getChinese() || "".equals(word.getChinese())) {
            result.put("message", "插入的单词中文为空或者为空字符串");
            return result;
        }

        wordRepository.save(word);//插入单词,插入数据使用save

        result.put("message", "插入单词成功");
        return result;
    }

    //根据id查询单词信息
    @GetMapping("/getWordById")
    @ResponseBody
    public Map<String, Object> getWordById(Integer id) {
        result = new HashMap<>();

        //数据校验 判空！
        if (null == id) {
            result.put("message", "传入id为空");
            return result;
        }

        result.put("word", wordRepository.findById(id));//按照id(主键)查询getOne()
        result.put("message", "查询单词成功");
        return result;
    }


    //根据id更新单词信息
    @PostMapping("/updateWordById")
    @ResponseBody
    public Map<String, Object> updateWordById(Integer id, String english, String chinese) {
        result = new HashMap<>();

        //数据校验 判空！
        if (null == id) {
            result.put("message", "更新的单词id为空");
            return result;
        }
        if (null == english || "".equals(english)) {
            result.put("message", "更新的单词中文为空或者为空字符串");
            return result;
        }
        if (null == chinese || "".equals(chinese)) {
            result.put("message", "更新的单词中文为空或者为空字符串");
            return result;
        }

        wordRepository.save(new Word(id, english, chinese));

        result.put("message", "更新单词成功");
        return result;
    }

    //根据id删除单词信息
    @PostMapping("/deleteWordById")
    @ResponseBody
    public Map<String, Object> deleteWordById(Integer id) {
        result = new HashMap<>();
        //数据校验 判空！
        if (null == id) {
            result.put("message", "输入的单词id为空");
            return result;
        }
        wordRepository.deleteById(id);//根据id删除单词信息
        result.put("message", "删除单词成功");
        return result;
    }

    //查询所有单词
    @GetMapping("/getAllWord")
    @ResponseBody
    public Map<String, Object> getAllWord() {
        result = new HashMap<>();
        result.put("wordList",wordRepository.findAll());//查询所有单词
        result.put("message", "查询所有单词成功");
        return result;
    }

}
